package com.tnt.springbootrabbmitmq.provider.topicexchange;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description 主题交换机消息生产者
 * @Author zhuangxl
 * @Date 2021-01-24 19:26
 */
@RestController
@RequestMapping("/topic/provider")
public class TopicExchangeProviderController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * @Description: 消息进入队列topicExchangQueue1 和 topicExchangQueue2 （符合路由key规则topicExchangQueue#）
     * @Author: zhuangxl
     * @Date: 21/1/24 20:29
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/sendMessage1")
    public Map<String, Object> sendTopicMessage1(){

        String messageId = UUID.randomUUID().toString();
        String messageContent = "I am Topic Exchange Message from sendTopicMessage1!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("messageId", messageId);
        paramsMap.put("messageContent", messageContent);
        paramsMap.put("createTime", createTime);

        rabbitTemplate.convertAndSend("tipicExchange","topicExchangeRoutingKey.1", paramsMap);

        return paramsMap;
    }

    /**
     * @Description: 消息进入队列 topicExchangQueue2（符合路由key规则topicExchangQueue#）
     * @Author: zhuangxl
     * @Date: 21/1/24 20:31
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/sendMessage2")
    public Map<String, Object> sendTopicMessage2(){

        String messageId = UUID.randomUUID().toString();
        String messageContent = "I am Topic Exchange Message from sendTopicMessage2!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("messageId", messageId);
        paramsMap.put("messageContent", messageContent);
        paramsMap.put("createTime", createTime);

        rabbitTemplate.convertAndSend("tipicExchange","topicExchangeRoutingKey.2", paramsMap);

        return paramsMap;
    }

    /**
     * @Description: 消息进入队列 topicExchangQueue2（符合路由key规则topicExchangQueue#）
     * @Author: zhuangxl
     * @Date: 21/1/24 20:32
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/sendMessage3")
    public Map<String, Object> sendTopicMessage3(){

        String messageId = UUID.randomUUID().toString();
        String messageContent = "I am Topic Exchange Message from sendTopicMessage3!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("messageId", messageId);
        paramsMap.put("messageContent", messageContent);
        paramsMap.put("createTime", createTime);

        rabbitTemplate.convertAndSend("tipicExchange","topicExchangeRoutingKey.3", paramsMap);

        return paramsMap;
    }
}
